----------------------------------Spell Variables--------------------------------------
SPELL.m_iSpellID = 15406 -- Whatever value you want as long as it doesn't override any other spells' IDs.
SPELL.m_iRange = 1024
SPELL.m_sName = "Jagged Shot"
SPELL.m_sToolTip =	[[You take aim, firing
					a jagged bolt into your
					target and causing heavy
					bleeding.]]
SPELL.m_sIcon = "devin/blackbetty/spells/ranger/jaggedshot.png"
SPELL.m_iCoolDown = 20
SPELL.m_iManaCost = 0
SPELL.m_bRequiresTarget = true
SPELL.m_iSpellType = SPELLTYPE_RANGEDAMAGE
SPELL.m_sUsedBy = "class_ranger"

---------------------------------------------------------------------------------------

function SPELL:Cast( pl, target )
	
	print("Casted "..self.m_sName.."!")
	
	local range = self.m_iRange
		
	if(target && target:IsValid() && target != pl) then
		pl:EmitSound("devin/blackbetty/weapons/bow/BowPullback0"..math.random(1,3)..".wav", 70, math.random(60, 70))
		timer.Simple(.9, function()
			pl:SetNWBool("Casting", false)
			pl:SetNWBool("Casted", false)
			pl.DoingCast = false
		end)
		net.Start( "SendPlayerCast" )
		local SendTable = {1.5, self.m_sName, Color(15, 15, 255, 155)}
		net.WriteTable(SendTable)
		net.Send(pl)
		pl.DoingCast = true
		pl.CastWhileMoving = true
		
		timer.Simple(1.5, function()
			if(pl:GetPos():Distance(target:GetPos()) < range)then
				pl:EmitSound("devin/blackbetty/weapons/bow/BowRelease0"..math.random(1,3)..".wav", 50, 70+math.random(-10,10))
				self:Effect(pl, target)
				--self:ApplySpellMods(self, pl, target)
			end
		end)
		else
			pl:SetNWBool("Casting", false)
			pl:SetNWBool("Casted", false)
			pl.DoingCast = false
			return true
	end
end

function SPELL:Effect(pl, target)

	local weapon = pl:GetWeapon("weapon_all")
	
	pl:DoAnimationEvent(ACT_HL2MP_GESTURE_RANGE_ATTACK_CROSSBOW)
	UpdatePlayerResource(pl, 20, true)
	if(SERVER)then
		ParticleEffect( "impact_firebig", target:GetPos() + Vector(0, 0, 32), Angle(0,0,0), pl )
		local myhitdelay = ((pl:GetPos() - target:GetPos()):Length()/2100)
		LaunchProjectile(pl, target, 0, "pummel_hit", "", "jaggedshot", "devin/blackbetty/weapons/travelsounds/FireMissileLoop.wav", myhitdelay, 2400, "models/crossbow_bolt.mdl")
		timer.Simple(myhitdelay, function()
			ParticleEffect( "blood_erupt", target:GetPos() + Vector(0, 0, 32), Angle(0,0,0), target )
			ParticleEffect( "blood_pool", target:GetPos() + Vector(0, 0, 5), Angle(0,0,0), target )
			ParticleEffectAttach( "blood_erupt_constant", PATTACH_POINT_FOLLOW, target, target:LookupAttachment("eyes"))
			local dmginfo = DamageInfo()
				if(weapon)then
					dmginfo:SetDamage( math.random(weapon.MinWeaponDamage*6, weapon.MaxWeaponDamage*6) )
				else
					dmginfo:SetDamage( math.random(pl.MinDamage, pl.MaxDamage) ) 
				end
				dmginfo:SetDamageType( DMG_BURN ) 
				dmginfo:SetAttacker( pl )
			target:SetVelocity(((pl:GetPos()- Vector(0, 0, 40)) - target:GetPos())*-2)
			target:TakeDamageInfo( dmginfo )
			target:EmitSound("devin/blackbetty/weapons/bow/ArrowHit0"..math.random(1,3)..".wav", 100, 100+math.random(-10,10))
			target:EmitSound("devin/blackbetty/weapons/RendTarget.wav", 150, 80)
			local totaldamage = weapon.MaxWeaponDamage*3
			if(target.ArmorStat and target.ArmorStat > 0)then
				totaldamage = totaldamage - target.ArmorStat
			end
			timer.Simple(3, function() target:StopParticles() end)
			if(timer.Exists("dw_on_"..target:EntIndex()..""))then
				timer.Destroy("dw_on_"..target:EntIndex().."")
				target:StopParticles()
			end
			timer.Create("dw_on_"..target:EntIndex().."", 1, 3, function() 
				if(target:Health() <= 0 or target:IsPlayer() and !target:Alive()) then 
					timer.Destroy("dw_on_"..target:EntIndex().."")  
				else
					local dmginfo_im = DamageInfo()
						dmginfo_im:SetDamage(math.ceil(totaldamage / 3)) 
						dmginfo_im:SetDamageType( DMG_SLASH )
						dmginfo_im:SetAttacker( pl ) 
						dmginfo_im:SetDamageForce( Vector( 0, 0, 1000 ) ) 
					target:TakeDamageInfo( dmginfo_im )
					ParticleEffect( "blood_erupt", target:GetPos() + Vector(0, 0, 48) + (target:GetAngles():Forward()*24), Angle(0,0,0), target )
				end
			end)
		end)
	end
end




